package cn.wizzer.app.web.modules.controllers.platform.crown;

import cn.wizzer.app.crown.modules.models.CrownConfig;
import cn.wizzer.app.crown.modules.models.constant.CrownRedisConstant;
import cn.wizzer.app.crown.modules.services.CrownConfigService;
import cn.wizzer.app.web.commons.ulog.annotation.SLog;
import cn.wizzer.framework.base.Result;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.integration.jedis.RedisService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpServletRequest;

/**
 * 爬虫账号管理
 *
 * @author
 */
@IocBean
@At("/platform/crown/config")
public class CrownConfigController {
    private static final Log log = Logs.get();
    @Inject
    @Reference
    private CrownConfigService crownConfigService;


    @Inject
    private RedisService redisService;

    @At("")
    @Ok("beetl:/platform/crown/config/index.html")
    @RequiresPermissions("crown.manager.config.list")
    public void index(HttpServletRequest req) {

    }

    /**
     * 查询账号列表（分页）
     *
     * @param searchKeyword String 条件搜索参数
     * @param searchDate    String[] 按搜索时间参数
     * @param pageNumber
     * @param pageSize
     * @return Object
     */
    @At
    @Ok("json:full")
    @RequiresPermissions("crown.manager.config.list")
    public Object data(@Param("searchKeyword") String searchKeyword,
                       @Param("searchDate") String[] searchDate,
                       @Param("pageNumber") int pageNumber,
                       @Param("pageSize") int pageSize) {
        return Result.success().addData(crownConfigService.data(searchKeyword, searchDate, pageNumber, pageSize));
    }

    /**
     * 添加账号
     *
     * @param crownConfig CrownConfig
     * @return Object
     */
    @At("/add")
    @Ok("json")
    @RequiresPermissions("crown.manager.config.add")
    @SLog(tag = "新建账号", msg = "账号用户名:${args[0].name}")
    public Object add(@Param("..") CrownConfig crownConfig) {
        // realSite默认与site相同
        crownConfig.setRealSite(crownConfig.getSite());
        crownConfigService.insert(crownConfig);
        // 存入redis
        redisService.set(CrownRedisConstant.CROWN_ACCOUNT + crownConfig.getUsername(), JSONObject.toJSONString(crownConfig));
        return Result.success();
    }

    /**
     * 手动登录皇冠账号
     *
     * @param crownConfig CrownConfig
     * @return Object
     */
    @At("/loginHg")
    @Ok("json")
    @RequiresPermissions("crown.manager.config.edit")
    public Object loginHg(@Param("..") CrownConfig crownConfig) {
        if (crownConfig.isDisabled()) {
            Result.breakWith("账号设置启用后再登录");
        }
        crownConfigService.loginHg(crownConfig);
        return Result.success();
    }


    /**
     * 根据id查询账号信息
     *
     * @param id 账号id
     * @return Object
     */
    @At("/edit/?")
    @Ok("json:full")
    public Object edit(@Param("id") String id) {
        CrownConfig crownConfig = crownConfigService.fetch(id);
        return Result.success(crownConfig);
    }

    /**
     * 修改账号信息
     *
     * @param crownConfig SysCrownConfig
     * @return Object
     */
    @At("/editDo")
    @Ok("json")
    @RequiresPermissions("crown.manager.config.edit")
    @SLog(tag = "修改账号", msg = "账号用户名:${args[1].getAttribute('username')}")
    public Object editDo(@Param("oldName") String oldName, @Param("..") CrownConfig crownConfig) {
        // 删除原账号(直接set覆盖有bug)
        redisService.del(CrownRedisConstant.CROWN_ACCOUNT + oldName);
        crownConfigService.editDo(crownConfig);
        return Result.success();
    }

    /**
     * 根据id启用账号
     *
     * @param id 账号id
     * @return Object
     */
    @At("/enable/?")
    @Ok("json")
    @RequiresPermissions("crown.manager.config.edit")
    @SLog(tag = "启用账号", msg = "账号用户名:${args[1].getAttribute('username')}")
    public Object enable(String id) {
        CrownConfig config = crownConfigService.fetch(id);
        config.setDisabled(false);
        crownConfigService.editDo(config);
        return Result.success();
    }

    /**
     * 根据id禁用账号
     *
     * @param id 账号id
     * @return Object
     */
    @At("/disable/?")
    @Ok("json")
    @RequiresPermissions("crown.manager.config.edit")
    @SLog(tag = "禁用账号", msg = "账号用户名:${args[1].getAttribute('username')}")
    public Object disable(String id) {
        CrownConfig config = crownConfigService.fetch(id);
        config.setDisabled(true);
        crownConfigService.editDo(config);
        return Result.success();
    }

    /**
     * 根据id删除账号
     *
     * @param id 账号id
     * @return Object
     */
    @At("/delete/?")
    @Ok("json")
    @RequiresPermissions("crown.manager.config.delete")
    public Object delete(String id) {
        CrownConfig crownConfig = crownConfigService.fetch(id);
        redisService.del(CrownRedisConstant.CROWN_ACCOUNT + crownConfig.getUsername());
        crownConfigService.delete(id);
        return Result.success();
    }

}
